﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEET.LEETCommon.Model;
using LEET.CommonInterfaces.Compilers;
using LEET.LEETCommon;
using System.IO;
using LEET.CommonInterfaces.TestControls;

namespace Tedds_Tool.Instructions
{
    public class Start : Instruction
    {
        private String exe_path;
        private String arguments;
        private String name;

        public Start(string [] args)
            : base(args)
        {
            exe_path = args[1].Trim().Replace("\\", "\\\\");
            int len = args.Length;
            if (len >= 2 && args[len - 2].Trim() == "as")
            {
                name = "\"" + args[len - 1].Trim() + "\"";
                len -= 2;
            }
            else
            {
                name =  "Box.MakeUnique(process.ProcessName)";
            }
            arguments = (len > 2) ? args[2].Trim().Replace("\\", "\\\\").Replace("\"", "\\\"") : null;
            InstructionType = NodeTypes.START_TYPE;
            Name = Path.GetFileName(exe_path.Trim());
        }

        public static IAutomationActionResult StaticCall()
        {
            //TODO
            return null;
        }

        public override String RenderStaticCall()
        {
            //TODO
            return "Tedds_Tool.Instructions.Start.StaticCall()";
        }


        [Obsolete("This should now be replaced by RenderStaticCall", false)]
        protected override String RenderedMethodName(int i)
        {
            return "_"+i+"_START";
        }

        [Obsolete("This should now be replaced by RenderStaticCall", false)]
        protected override String MethodBody(AssertionSet asserts)
        {
            StringBuilder buf = new StringBuilder();
            buf.Append("                String exe = \"").Append(exe_path).Append("\";\r\n");

            buf.Append("                Process process = Utilities.Start_Process(exe");
            buf.Append((arguments == null ? "" : ", \"" + arguments + "\""));
            buf.Append(");\r\n");
            buf.Append("                return new AutomationActionResult(new ProcessWrapper(process, "+name+"));\r\n");  //Needed for yield return.  Perhaps the state should be returned instead?
            
            return buf.ToString();
        }

        public override string PlainText()
        {
            return "Started " + exe_path + ".";
        }
    }
}
